#!/bin/bash

# Set project root directory
sim_root="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
simulator_dir="$sim_root/src/simulator"

# Create target directory if it doesn't exist
target_dir="$simulator_dir/target/debug"
mkdir -p "$target_dir"

# Source file and output paths
src_file="$simulator_dir/benches/benchmark_chip.rs"
output_file="$target_dir/benchmark_chip"

# Function to find library paths dynamically
find_crate_path() {
    local crate_name=$1
    local cargo_home=${CARGO_HOME:-$HOME/.cargo}
    local lib_path
    
    # Try to find the compiled library in cargo registry
    lib_path=$(find "$cargo_home/registry/src" -name "lib${crate_name}-*.rlib" 2>/dev/null | head -1)
    if [ -n "$lib_path" ]; then
        echo "$lib_path"
        return 0
    fi
    
    # Try to find in target directory
    lib_path=$(find "$simulator_dir/target/debug/deps" -name "lib${crate_name}-*.rlib" 2>/dev/null | head -1)
    if [ -n "$lib_path" ]; then
        echo "$lib_path"
        return 0
    fi
    
    echo "Warning: Could not find $crate_name library"
    return 1
}

# Function to find macro library paths dynamically
find_macro_path() {
    local crate_name=$1
    local cargo_home=${CARGO_HOME:-$HOME/.cargo}
    local lib_path
    
    # Try to find the compiled macro library
    lib_path=$(find "$cargo_home/registry/src" -name "lib${crate_name}.so" 2>/dev/null | head -1)
    if [ -n "$lib_path" ]; then
        echo "$lib_path"
        return 0
    fi
    
    # Try to find in dam-macros target directory
    lib_path=$(find "$simulator_dir/dam-macros/target/debug" -name "lib${crate_name}.so" 2>/dev/null | head -1)
    if [ -n "$lib_path" ]; then
        echo "$lib_path"
        return 0
    fi
    
    echo "Warning: Could not find $crate_name macro library"
    return 1
}

# Print build information
echo "Compiling benchmark_chip.rs..."
echo "Source Root: $simulator_dir"
echo "Source: $src_file"
echo "Output: $output_file"

# Find dependency paths
echo "Finding dependency paths..."
criterion_path=$(find_crate_path "criterion")
dam_path=$(find_crate_path "dam")
dam_macros_path=$(find_macro_path "dam_macros")
serde_path=$(find_crate_path "serde")
serde_json_path=$(find_crate_path "serde_json")
thiserror_path=$(find_crate_path "thiserror")

# Build rustc command with found paths
rustc_cmd="rustc \
    --edition=2021 \
    --crate-type=bin \
    --crate-name=benchmark_chip \
    -o \"$output_file\" \
    -L \"$simulator_dir/target/debug/deps\""

# Add --extern flags for found libraries
if [ -n "$criterion_path" ]; then
    rustc_cmd+=" \
    --extern criterion=\"$criterion_path\""
fi

if [ -n "$dam_path" ]; then
    rustc_cmd+=" \
    --extern dam=\"$dam_path\""
fi

if [ -n "$dam_macros_path" ]; then
    rustc_cmd+=" \
    --extern dam_macros=\"$dam_macros_path\""
fi

if [ -n "$serde_path" ]; then
    rustc_cmd+=" \
    --extern serde=\"$serde_path\""
fi

if [ -n "$serde_json_path" ]; then
    rustc_cmd+=" \
    --extern serde_json=\"$serde_json_path\""
fi

if [ -n "$thiserror_path" ]; then
    rustc_cmd+=" \
    --extern thiserror=\"$thiserror_path\""
fi

rustc_cmd+=" \
    \"$src_file\""

# Execute the compilation command
echo "Running compilation command..."
echo "$rustc_cmd"
eval "$rustc_cmd"

# Check if compilation was successful
if [ $? -eq 0 ]; then
    echo "Compilation successful! Executable located at: $output_file"
else
    echo "Compilation failed!"
    echo "Suggestions:"
    echo "1. Make sure all dependencies are installed: cd $simulator_dir && cargo build"
    echo "2. Check if the source file has any issues"
    exit 1
fi